<?php
/*
  Copyright (c) 2010-02 SongCMS
  SongCMS All Rights Reserved.
  Support:www.SongCMS.com
  QQ:544255858   19951025
  Author:Song  Version:3.17
  Date:2010-08-12 09:28:32
*/
function FSubstr($string,$start,$length,$symbol=' ..',$code='utf-8'){
	$sy = $code=='utf-8' ? 3 : 2;
	$chars = $string;
	$i=0;
	while($k < $length){
		if (preg_match ("/[0-9a-zA-Z]/", $chars[$i])){
			$m++;
		} else {
			$n++;
		}
		$k = $n/$sy+$m/2;
		$l = $n/$sy+$m;
		$i++;
	}
	$str1 = iconv_substr($string,$start,$l,$code);
	if((strlen($string)-$m-$n) > 0) $str1 .= $symbol;
	return $str1;
}
function MsgBox($Dialog,$Text,$Url){
	if ($Url == '1') {$Url = $_SERVER['REQUEST_URI'];} else if ($Url == '0'){$Url = $_SERVER['SCRIPT_NAME'];}
	echo '<script language="javascript">';
	if ($Dialog == 1){echo 'alert("' . $Text . '");';}
	if ($Url == "-1"){
		echo 'history.back(-1);';
	}elseif ($Url == "2"){
		echo 'window.opener=null; window.open("","_self"); window.close();';
	}else{
		echo 'location="' . $Url . '";';
	}
	echo '</script>';
	exit();
}
function WebConfig($Field,$Lang=1){
	global $db,$dbprefix;
	$WebConfig = $db->SelectSQL("SELECT " . $Field . " FROM `" . $dbprefix . "webconfig` WHERE WebLang = " . $Lang);
	return strstr($Field,',') ? $WebConfig : $WebConfig[0][0];
}
function External(){
	return true;
	global $_SERVER;
	$HOST = isset($_SERVER['HTTP_X_FORWARDED_HOST']) ? $_SERVER['HTTP_X_FORWARDED_HOST'] : (isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : '');
	$Host1 = strtolower($HOST);
	$Host2 = strtolower($_SERVER['HTTP_REFERER']);
	$Host2 = parse_url($Host2);
	$Host2 = $Host2['host'];
	if ($Host1==$Host2){return true;} else {return false;}
}
function ParentID($Rela,$head=0){
	$Rela = explode('|',$Rela);
	return $head==0 ? count($Rela)<=1 ?  '' : $Rela[count($Rela)-2] : $Rela[0];
}
function AdminLog($LogType=1,$LogLang=1,$LogDetails=''){
	global $ConfigAdminLog,$_SERVER,$db,$dbprefix;
	if($ConfigAdminLog != 1){return;}
	$LogUser = @$_SESSION['UserName'];
	$LogIP = @$_SERVER['REMOTE_ADDR'];
	$LogPage = @$_SERVER['REQUEST_URI'];
	$SQL = "INSERT INTO `" . $dbprefix . "adminlog` (`LogType`,`LogLang` ,`LogUser`,`LogIP`,`LogTime`,`LogDetails`,`LogPage`) ";
	$SQL .= "VALUES ('$LogType','$LogLang','$LogUser','$LogIP',NOW() ,'$LogDetails','$LogPage');";
	$db -> ExecuteSQL($SQL);
}
function pages($Urlpar='',$Lang='Cn',$aColor='#999999'){
	global $page,$PageCount,$RsCount;
	$RsCount % $PageCount != 0 ? $TotalPages = (int)($RsCount/$PageCount+1) : $TotalPages = $RsCount/$PageCount;
	if($page>$TotalPages){$page=$TotalPages;}
	$Urlpar=='' ? $Url='?page=' : $Url='?' . $Urlpar . '&page=';
	switch ($Lang){
		case 'Cn' :
			$pages = '共<font color="#FF0000">' . $RsCount . '</font>条&nbsp;|&nbsp;';
			$First='首页'; $Previous='上一页'; $Next='下一页'; $Last='尾页';
			$Records = '<font color="#FF0000">' . $PageCount . '</font>条/页&nbsp;|&nbsp;';
			$sDpage = '共<font color="#FF0000">' . $TotalPages . '</font>页' . "\n";
			$sDpage .= '<select name="spage" id="spage" OnChange="window.open(this.options[this.selectedIndex].value,\'_self\')">' . "\n";
			for ($i=1;$i<=$TotalPages;$i++){
				if ($i!=$page){
					$sDpage .= '<option value="' . $Url . $i . '">第' . $i . '页</option>' . "\n";
				}else{
					$sDpage .= '<option selected value="' . $Url . $i . '">第' . $i . '页</option>' . "\n";
				}
			}
			$sDpage .= '</select>';
			break;
		case 'En' :
			$pages = 'Total <font color="#FF0000">' . $RsCount . '</font> Records&nbsp;|&nbsp;';
			$First='First'; $Previous='Previous'; $Next='Next'; $Last='Last';
			$Records = '<font color="#FF0000">' . $PageCount . '</font>/page&nbsp;|&nbsp;';
			$sDpage = 'Page';
			$sDpage .= '<select name="spage" id="spage" OnChange="window.open(this.options[this.selectedIndex].value,\'_self\')">' . "\n";
			for ($i=1;$i<=$TotalPages;$i++){
				if ($i!=$page){
					$sDpage .= '<option value="' . $Url . $i . '">' . $i . '</option>' . "\n";
				}else{
					$sDpage .= '<option selected value="' . $Url . $i . '">' . $i . '</option>' . "\n";
				}
			}
			$sDpage .= '</select>' . "\n";
			$sDpage .= 'Of ' . $TotalPages;
			break;
	}
	$page <= 1 ? $pages .= '<font color="' . $aColor . '">' . $First . '</font>&nbsp;|&nbsp;' : $pages .= '<a href="' . $Url . '1">' . $First . '</a>&nbsp;|&nbsp;' . "\n";
	$page <= 1 ? $pages .= '<font color="' . $aColor . '">' . $Previous . '</font>&nbsp;|&nbsp;' : $pages .= '<a href="' . $Url . ($page-1) . '">' . $Previous . '</a>&nbsp;|&nbsp;' . "\n";
	$page >= $TotalPages ? $pages .= '<font color="' . $aColor . '">' . $Next . '</font>&nbsp;|&nbsp;' : $pages .= '<a href="' . $Url . ($page+1) . '">' . $Next . '</a>&nbsp;|&nbsp;' . "\n";
	$page >= $TotalPages ? $pages .= '<font color="' . $aColor . '">' . $Last . '</font>&nbsp;|&nbsp;' : $pages .= '<a href="' . $Url . $TotalPages . '">' . $Last . '</a>&nbsp;|&nbsp;' . "\n";
	return $pages .= $Records . $sDpage;
	unset($page,$PageCount,$RsCount,$First,$Previous,$Next,$Last,$Records,$sDpage,$i,$TotalPages);
}
function ArticleName($aArray,$blank=' → ',$isURL=0){
	global $db,$dbprefix;
	if(empty($aArray)) return;
	$aArray = explode('|',$aArray);
	$nRs = $db->SelectSQL("SELECT * FROM `" . $dbprefix . "column` WHERE ID IN('". join('\',\'',$aArray) ."') ORDER BY `ColumnRela` ASC",1);
	foreach ($nRs as $i => $n){
		if(count($nRs) == ($i+1)) $blank = NULL;
		if($isURL == 1){
			@$ArticleName .= '<a href="'. sdb::CidToName_URL($n['ColumnType'],$n['ID']) .'">'.$n['ColumnName'] .'</a>'. $blank;
		} else {
			@$ArticleName .= $n['ColumnName'] . $blank;
		}
	}
	unset($aArray,$blank,$nRs,$i,$n);
	return $ArticleName;
}
function Loading($ImgUrl='../images/Loading.gif'){echo '<img src="' . $ImgUrl . '" width="16" height="16" /> 数据加载中，请稍候……';}
function FilesSize($path='./',$units=1){
	if(!file_exists($path)) return 0;
	$units = $units=='KB' ? 1024 : ($units=='MB' ? 1048576 : 1);
	if(is_file($path)) return number_format((filesize($path)/$units),2,'.','');
	$self = __FUNCTION__;
	$ret = 0;
	foreach(glob($path."/*") as $fn){
		$ret += $self($fn);
	}
	$ret = number_format(($ret/$units),2,'.','');
	return $ret;
}
function removeDir($path){
	$result = true;
	if (substr($path, -1, 1) != "/") $path .= "/";
	foreach (glob($path."*") as $file){
		if (is_file($file)){
			if(!unlink($file)) $result = false;
		} elseif (is_dir($file)){
			removeDir($file);
		}
	}
	if (is_dir($path)){
		if(!rmdir($path)) $result = false;
	}
	return $result;
}
function Extend($file_name){
	$Extend = pathinfo($file_name);
	$Extend = strtolower($Extend["extension"]);
	return $Extend;
}
function Html(&$array){
	if(is_array($array)){
		foreach ($array as $key => $value) {
			$array[$key] = htmlspecialchars($value);
		}
	}else{
		$array = htmlspecialchars($array);
		return $array;
	}
}
function MyScandir($FilePath='./',$Order=0){
	$FilePath = opendir($FilePath);
	while (false !== ($filename = readdir($FilePath))) {
		$FileAndFolderAyy[] = $filename;
	}
	$Order == 0 ? sort($FileAndFolderAyy) : rsort($FileAndFolderAyy);
	return $FileAndFolderAyy;
}
function LangUrlExtract($parms){
    $db = new db();

    $parms_tmp = explode('/', $parms);
    foreach ($parms_tmp as $_key =>$_value){
        $Query[substr($_value,0,1)] = substr(trim($_value),1);
    }
    $Query['l']?null:$Query['l']=1;
    if($Query['f']=='new'){
        $type = 'NewsLang';
        $table = 'song_news';
    }elseif($Query['f']=='product'){
        $type = 'ProductLang';
        $table = 'song_product';
    }
    $Sql = "SELECT $type,lang_relative FROM $table WHERE ID = " . $Query['i'];
    $Info = $db->SelectSQL($Sql);

    if($Info){
        if( $Query['l'] != $Info[0][$type]){
            $lang_relative = explode(',',$Info[0]['lang_relative']);
            $lang_key = $Query['l']-1;
            if($lang_relative[$lang_key]){
                $Query['i'] = $lang_relative[$lang_key];
            }
        }
    }
    foreach ($Query as $_key=>$_value){
        $A_query[] = $_key.$_value;
    }
    $parms = implode('/', $A_query);
    return $parms;
}
?>